'''
Author: gaoh
Date: 2023-07-19 18:45:27
LastEditTime: 2023-07-19 23:57:02
'''
import urllib.request
import urllib.parse
import urllib.error
import socket

from urllib import request, parse, error

# -------------------------------------1-------------------------------------

# response = urllib.request.urlopen('https://www.python.org')
# # print(response.read().decode('utf-8'))
# print(type(response))
# print(response.status)
# print(response.getheaders())
# print(response.getheader('Server'))

# -------------------------------------2-------------------------------------


# data=bytes(urllib.parse.urlencode({'work':'hello'}), encoding='utf-8')
# resp=urllib.request.urlopen('http://httpbin.org/post',data=data)
# print(resp.read())


# try:
#     sp=urllib.request.urlopen('http://httpbin.org/get',timeout=0.1)
# except urllib.error.URLError as e:
#     if isinstance (e.reason,socket.timeout):
#         print('timeout...')

# -------------------------------------3-------------------------------------

# try:
#     response = urllib.request.urlopen('http://httpbin.org/get', timeout=0.1)
# except urllib.error.URLError as e:
#     if isinstance(e.reason, socket.timeout):
#         print('TIME OUT')

# -------------------------------------4-------------------------------------

# url = 'http://httpbin.org/post'
# headers = {'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',
#            'Host': 'httpbin.org'
#            }
# dist = {'name': 'Germey'}
# data = bytes(parse.urlencode(dist), encoding='utf-8')
# req = request.Request(url, data=data, headers=headers, method='POST')
# resp = request.urlopen(req)
# print(resp.read().decode('utf-8'))


# -------------------------------------Auth-------------------------------------

# from urllib.request import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, build_opener
# from urllib.error import URLError

# username = 'username'
# password = 'password'
# url = 'http://localhost:5000/'

# p = HTTPPasswordMgrWithDefaultRealm()
# p.add_password(None, url, username, password)
# auth_handler = HTTPBasicAuthHandler(p)
# opener = build_opener(auth_handler)

# try:
#     result = opener.open(url)
#     html = result.read().decode('utf-8')
#     print(html)
# except URLError as e:
#     print(e.reason)


# -------------------------------------Proxy-------------------------------------

from urllib.error import URLError
from urllib.request import ProxyHandler, build_opener

proxy_handler = ProxyHandler({
    'http': 'http://127.0.0.1:7890',
    'https': 'https://127.0.0.1:7890'
})
opener = build_opener(proxy_handler)
try:
    response = opener.open('https://www.baidu.com')
    print(response.read().decode('utf-8'))
except URLError as e:
    print(e.reason)


# -------------------------------------Cookies-------------------------------------
# import http.cookiejar, urllib.request  

# cookie = http.cookiejar.CookieJar()  
# handler = urllib.request.HTTPCookieProcessor(cookie)  
# opener = urllib.request.build_opener(handler)  
# response = opener.open('http://www.baidu.com')  
# for item in cookie:  
#     print(item.name+"="+item.value)

# # 保存cookie
# filename = 'cookies.txt'  
# # cookie = http.cookiejar.MozillaCookieJar(filename) 
# #  LWP
# cookie = http.cookiejar.LWPCookieJar(filename)  
# handler = urllib.request.HTTPCookieProcessor(cookie)  
# opener = urllib.request.build_opener(handler)  
# response = opener.open('http://www.baidu.com')  
# cookie.save(ignore_discard=True, ignore_expires=True)


# # 读取LWP

# cookie = http.cookiejar.LWPCookieJar()  
# cookie.load('cookies.txt', ignore_discard=True, ignore_expires=True)  
# handler = urllib.request.HTTPCookieProcessor(cookie)  
# opener = urllib.request.build_opener(handler)  
# response = opener.open('http://www.baidu.com')  
# print(response.read().decode('utf-8'))